#!/usr/bin/env bash
VERSION=$1
[ -z "$VERSION" ] && exit 1
OS=`uname`
SED_ARG='-r'

if [ $OS == "Darwin" ]
then
  SED_ARG='-E'
fi

sed $SED_ARG "s/version = (.*)/version = \"$VERSION-1\"/;s/tag = (.*)/tag = \"v$VERSION\"/" rockspecs/elasticsearch.rockspec >rockspecs/elasticsearch-$VERSION-1.rockspec
sed $SED_ARG "s/luarocks make (.*)/luarocks make rockspecs\/elasticsearch-$VERSION-1.rockspec/" .travis.yml >.travis.yml.tmp
sed $SED_ARG "s/luarocks make (.*)/luarocks make rockspecs\/elasticsearch-$VERSION-1.rockspec/" tests/stress/.travis.yml >tests/stress/.travis.yml.tmp

if [ $? == 0 ]
then
  mv .travis.yml.tmp .travis.yml
  mv tests/stress/.travis.yml.tmp tests/stress/.travis.yml
  git add rockspecs/elasticsearch-$VERSION-1.rockspec
  read -r -p "Do you want to tag this release in git? [y/N] " response
  if [[ $response =~ ^([yY])$ ]]
  then
    SIGN=""
    read -r -p "Do you want to sign the tag? [y/N] " response
    if [[ $response =~ ^([yY])$ ]]
    then
      SIGN="--sign"
    fi
    git add -u 
    git commit -m "Set version $VERSION and tagged as v$VERSION"
    git tag -a v$VERSION -m "v$VERSION" $SIGN
    echo "Tagged this commit as v$VERSION"
  fi
fi
